<!DOCTYPE html>
<html lang="zh-CN">

<head>
  <meta charset="UTF-8">
  <meta name="author" content="wangbo">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>输入框</title>
  <style>

  </style>
</head>

<body>
  <table border=1 width=200px height=100px align="center" cellpadding=0 cellspacing=0>
    <thead>
      <tr>
        <th id="all_check"><input type="checkbox" id="j_cbAll">全选</th>
        <th>商品</th>
        <th>价格</th>
      </tr>
    </thead>
    <tbody id="j_tb">
      <tr>
        <td><input type="checkbox"></td>
        <td>苹果11</td>
        <td>5800</td>
      </tr>
      <tr>
        <td><input  type="checkbox"></td>
        <td>苹果13</td>
        <td>7899</td>
      </tr>
      <tr>
        <td><input type="checkbox"></td>
        <td>荣耀20</td>
        <td>2899</td>
      </tr>
    </tbody>
  </table>
  <script>
    var inpArr = document.querySelectorAll("#j_tb input")
    var inp_all = document.querySelector("#j_cbAll")
    // console.log(inp_all.checked)
    function checkAll() {
      for (let i = 0; i < inpArr.length; i++) {
        inpArr[i].checked = inp_all.checked
        // if (inp_all.checked) {
        //   inpArr[i].setAttribute("checked","")
        // }else{
        //   inpArr[i].removeAttribute("checked")
        // }
      }
    }
    inp_all.addEventListener("click",checkAll)
    // console.log(inpArr)
    for (let i = 0; i < inpArr.length; i++) {
      inpArr[i].onclick = function () {
        let inpArr2 = document.querySelectorAll("#j_tb input:checked")
        inp_all.checked = inpArr2.length ==inpArr.length
        // if (inpArr2.length ==inpArr.length ) {
        //   inp_all.checked = true
        // }else{
        //   inp_all.checked = false
        // }        
      }
    }
  </script>
</body>

</html>